package cn.apthink.controller.system;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.apthink.controller.BaseResult;
import cn.apthink.controller.DataResult;
import cn.apthink.controller.ListResult;
import cn.apthink.model.AUser;
import cn.apthink.model.AUserRole;
import cn.apthink.model.VUserRole;
import cn.apthink.model.UUser;
import cn.apthink.mybatisplus.entity.UserEntity;
import cn.apthink.mybatisplus.entity.UserRoleEntity;
import cn.apthink.mybatisplus.service.RoleService;
import cn.apthink.mybatisplus.service.UserService;
import cn.apthink.mybatisplus.service.UserRoleService;
import cn.apthink.service.AuthService;
import cn.apthink.solon.Logging;
import cn.apthink.utility.ObjectUtil;
import cn.apthink.utility.Util;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户
 *
 * @author David唐
 * @createdate 2024年1月18日
 */
@Logging
@Slf4j
@Controller
@Api(value="用户控制器")
@Mapping("/api/user")
public class UserController {
	@Inject
	UserService userService;
	@Inject
	RoleService roleService;
	@Inject
	UserRoleService userRoleService;
	@Inject
	AuthService authService;

	@Mapping("/delete")
	@ApiOperation("删除指定用户信息")
	@SaCheckPermission("user#delete")
	public BaseResult delete(@ApiParam(name = "id", value = "用户id", required = true) String id) {
		UserEntity user = userService.getById(id);
		if (Util.isEmpty(user)) {
			return DataResult.error(99,  "用户数据不存在", "user");
		}
		QueryWrapper<UserRoleEntity> q = new QueryWrapper<>();
		q.eq("userid", id);
		userRoleService.remove(q);
		userService.removeById(id);
		return DataResult.success("删除用户成功", user);
	}

	@Mapping("/add")
	@ApiOperation("新增用户信息")
	@SaCheckPermission("user#add")
	public BaseResult add(@ApiParam(name = "AUser", value = "用户信息", required = true) AUser data) {
		if (Objects.isNull(data.getAccount())) {
			return DataResult.error(101, "账户名不能为空", "account");
		}
		UserEntity user = new UserEntity();
		ObjectUtil.copy(data, user);
		userService.save(user);
		return DataResult.success("新增用户成功", user);
	}

	@Mapping("/update")
	@ApiOperation("修改用户信息")
	@SaCheckPermission("user#update")
	public BaseResult update(@ApiParam(name = "UUser", value = "用户信息", required = true) UUser data) {
		if (Objects.isNull(data.getId())) {
			return DataResult.error(101, "id不能为空", "id");
		}
		if (Objects.isNull(data.getAccount())) {
			return DataResult.error(101, "账户名不能为空", "account");
		}
		UserEntity user = userService.getById(data.getId());
		if (Objects.isNull(user)) {
			return DataResult.error(99,  "用户数据不存在", "user");
		}
		ObjectUtil.copy(data, user);
		userService.updateById(user);
		return DataResult.success("变更用户成功", user);
	}

	@Mapping("/updaterole")
	@ApiOperation("修改用户角色信息")
	@SaCheckPermission("user#update")
	public BaseResult updaterole(@Body @ApiParam(name = "AUserRole", value = "用户角色信息", required = true) AUserRole userRole) {
		log.debug(userRole.toString());
		if (Objects.isNull(userRole.getUserid())) {
			return DataResult.error(101, "用户id不能为空", "userid");
		}
		UserEntity user = userService.getById(userRole.getUserid());
		if (Objects.isNull(user)) {
			return DataResult.error(99, "用户数据不存在", "user");
		}
		if (Objects.isNull(userRole.getRoles())) {
			return DataResult.error(101, "角色id对象集不能为null", "roles");
		}
		List<UserRoleEntity> urs = new ArrayList<>();
		userRole.getRoles().forEach(x->{
			UserRoleEntity ur = new UserRoleEntity();
			ur.setUserid(userRole.getUserid());
			ur.setRoleid(x);
			urs.add(ur);
		});

		QueryWrapper<UserRoleEntity> q = new QueryWrapper<>();
		q.eq("userid", userRole.getUserid());
		userRoleService.remove(q);

		userRoleService.saveBatch(urs);
    	return ListResult.success(urs);
	}

    @Mapping("/all")
    @ApiOperation("所有用户信息")
    public ListResult<UserEntity> all() {
    	log.debug("all");
    	return ListResult.success(userService.list());
    }

    @Mapping("/defaultdata")
    @ApiOperation("缺省用户信息")
    public ListResult<UserEntity> defaultdata() {
    	log.debug("defaultdata");
		List<String> dd = Arrays.asList("aps","mgt", "administrator");
		List<UserEntity> ds = userService.listByIds(dd);
    	ListResult<UserEntity> result = new ListResult<>();
    	result.setData(ds);
    	return result;
    }

    @Mapping("/userrole")
    @ApiOperation("所有用户角色信息")
    public ListResult<VUserRole> userrole() {
    	log.debug("all");
    	return ListResult.success(userRoleService.selectQUserRole());
    }

}